<?php

namespace frontend\models;

use Yii;
use backend\models\Product;

//use common\models\User;
/**
 * This is the model class for table "recentView".
 *
 * @property string $recentViewId
 * @property string $productId
 * @property integer $userId
 * @property string $createTime
 *
 * @property Product $product
 * @property User $user
 */
class RecentView extends superModel {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'recentView';
    }

    public static function getCTableName() {
        return 'recentView';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
                [['productId', 'userId'], 'integer'],
//                [['createTime'], 'required'],
            [['createTime'], 'safe'],
                [['productId'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['productId' => 'productId']],
//                [['userId'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['userId' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'recentViewId' => 'ID',
            'productId' => '商品',
            'userId' => '用户',
            'createTime' => '创建时间',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProduct() {
        return $this->hasOne(Product::className(), ['productId' => 'productId']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'userId']);
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            $this->createTime = RecentView::getDateTime();
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 功能描述
     * @param $id int
     * @param $picturesAddress string 图片地址
     * @param $type boolean 选择链接的方式还是ajax的方式
     * @throws HttpException
     * @return string
     */
    public function getRecentViewProductList() {
        $recentViewModels = RecentView::find()->where(['userId' => Yii::$app->user->id])->orderby('createTime desc');
        $recentViewList = $recentViewModels->all();
        $recentViewProductList = array();
        foreach ($recentViewList as $recentViewUnit) {
            $recentViewProductList[] = $recentViewUnit->getProduct()->one();
        }
        //大于6条就删除
        if ($recentViewModels->count() > 6) {
            $recentViewList['6']->delete();
        }

        return $recentViewProductList;
    }

    /**
     * 功能描述
     * @param $id int
     * @param $picturesAddress string 图片地址
     * @param $type boolean 选择链接的方式还是ajax的方式
     * @throws HttpException
     * @return string
     */
    public function addRecentView($productId) {
        $recentViewModel = RecentView::find()->where(['userId' => Yii::$app->user->id, 'productId' => $productId])->one();
        //最近浏览列表里面没有，是条新纪录
        if ($recentViewModel == NULL) {
            $recentViewModel = new RecentView();
            //插入一条最近浏览
            $recentViewModel->userId = Yii::$app->user->id;
            $recentViewModel->productId = $productId;
            if ($recentViewModel->save()) {
                
            } else {
                echo "<pre>";
                var_dump($recentViewModel->errors);
                echo "</pre>";
                exit;
            }
        }
    }
}